package mya.mes.mdc.model.tag.convert;


import mya.mes.mdc.model.tag.Shop;

class PlcTagParser {

    private final String[] postfixArray = new String[]{
            "_ENTRY_PHOTOCELL",
            "_EXIT_PHOTOCELL",
            "_ENTRY_PHOTOCELL_A",
            "_ENTRY_PHOTOCELL_B",

            "_ENTRY_PHOTOCELL_C",
            "_EXIT_PHOTOCELL_A",
            "_EXIT_PHOTOCELL_B",
            "_EXIT_PHOTOCELL_C",
            "_FORWARD",
            "_REVERSE",

            "_FIX_BELT_AC_BD",
            "_FIX_BELT_AC_BD_BD01",
            "_FIX_BELT_CA_DB",
            "_MOBIL_BELT_AC_BD",
            "_MOBIL_BELT_CA_DB",

            "_AC_BD",
            "_CA_DB",
            "_AC",
            "_CA",
            "_BD",
            "_DB",
            "_DOWN",
            "_UP",
            "_ATR",
            "_FTR",
            "_READ",
            "_START_RUNNING",
            "_STOP_RUNNING",
            "_BELT_1_2",
            "_BELT_2_1",

            "_START_WEIGHING_PHOTOCELL",
            "_WEIGHING_PHOTOCELL",
            "_WEIGHT_RESET_PHOTOCELL",
            "_PRESENCE_PHOTOCELL",
            "_SIDE_1_PHOTOCELL",
            "_SIDE_2_PHOTOCELL",
            "_ENABLE",

            "_RECUL",
            "_SATURATION_PHOTOCELL",
            "_BACKWARD",
            "_ENTRY_A_PHOTOCELL",
            "_ENTRY_B_PHOTOCELL",
            "_ENTRY_C_PHOTOCELL",

            "_ANSWER.V_UNIT_DROP_OFF",
            "_ANSWER.V_POST_1",
            "_BELT_FORWARD_BD01",
            "_ENTRY_PHOTOCELL_D",


            "_PHOTOCELL_A",
            "_PHOTOCELL_B",
            "_PHOTOCELL_C",

            "_ENTRY_PHOTOCELL_1",
            "_EXIT_PHOTOCELL_1",

            "_ENTER_PHOTOCELL",
            "_MID_PHOTOCEL_LIFT",

            "B_OUT_BELT_DOWN",
            "B_OUT_BELT_UP",
            "B_OUT_BELT_BACKWARD",
            "B_OUT_BELT_FORWARD",
            "I_INPUT_CELL",
            "I_OUTPUT_CELL",
            "I_OUTBOUND_CELL",
            "I_PRESENCE_CELL",
            "I_FORK1_PRESENCE_CELL",
            "I_FORK2_PRESENCE_CELL",
            "_FORK1_CELL",
            "_FORK2_CELL",
            "_B_COMMAND_RUNNING",
            "_COMMAND_COMPLETE",
            "_FORK1_DETACT",
            "_FORK2_DETACT",

            "I_CEL_ENTREE_A",
            "I_CEL_ENTREE_B",
            "I_CEL_ENTREE_C",
            "I_CEL_ENTREE",
            "I_CEL_SORTIE",
            "I_CEL_PRESENCE",
            "I_CEL_SECU_SIDE_1",
            "I_CEL_SECU_SIDE_2",

            "_SIDE_1_SECURITY_PHOTOCELL",
            "_SIDE_2_SECURITY_PHOTOCELL",
            "I_CEL_SECU_SIDE_3",

            "I_Barcode_Read",

            "V_NB_LECTURE",
            "V_NB_LECTURE_KO",
            "V_TAUX_DE_LECTURE",
            "V_NB_READING",
            "V_BAD_READ_COUNT",
            "V_GOOD_READ_RATE",
            "R006_BS_EXIT_CONVEYOR_RUN_CMD",
            "R006_BS_FEED_CONVEYOR_RUN_COMMAND",
            "R005_B_SPRAYPOST_I",
            "R005_B_SPRAYPOST_IN_AUTO",

            "I_CAB_LU",

            "_MAINTENANCE",
            "_PROD",
            "_PRODUCTION",
            "ENTRY_0B_PHOTOCELL",
            "ENTRY_90A_PHOTOCELL",
            "ENTRY_90C_PHOTOCELL",

//    "SEND_LOGIC"
    };


    private final String[] digitsPostfixArray = new String[]{
            "_DIGITS_21",
            "_DIGITS_43",
            "_DIGITS_65",
            "_DIGITS_87"
    };

    private final String[] postePostfixArray = new String[]{
            "_POSTE1",
            "_POSTE2",
            "_POSTE3"
    };

    public MdcTagTmp parse(String shop, String area, String plcAddress) {
//    if("PROGRAM:SECTION1.TC101_EN4_Entry_Photocell_B".equals(plcAddress)) {
//      System.out.println("sdfsdf");
//    }
        plcAddress = plcAddress.trim().toUpperCase();
        int dataIndex = plcAddress.indexOf("S_AFF_INFO");

        if (dataIndex > 0) {
            String tmp = plcAddress.substring(0, dataIndex);
            int splitIndex = tmp.lastIndexOf('_');
            tmp = tmp.substring(splitIndex + 1);
            if (tmp.charAt(tmp.length() - 1) == '.') {
                tmp = tmp.substring(0, tmp.length() - 1);
            }
            MdcTagTmp mdcTagTmp = new MdcTagTmp();

            mdcTagTmp.type = PlcTagType.DATA;
            mdcTagTmp.viewTag = tmp;
            return mdcTagTmp;
        }

        if (plcAddress.endsWith("Send_Logic".toUpperCase())) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();

            mdcTagTmp.type = PlcTagType.DATA;
            mdcTagTmp.viewTag = "EL2102";
            return mdcTagTmp;
        }
        if (Shop.CUR.equals(shop)) {
            if ("BFG1".equals(area)) {
                if (plcAddress.equals("R_BS[5]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG1_EXIT;
                    mdcTagTmp.viewTag = "BFG1_EXIT";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_BS[158]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG1_FEED;
                    mdcTagTmp.viewTag = "BFG1_FEED";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_B[1]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG1_GREEN;
                    mdcTagTmp.viewTag = "BFG1_MANUAL";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_HMI_IS[12].0")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG1_GREEN;
                    mdcTagTmp.viewTag = "BFG1_GREEN";
                    return mdcTagTmp;
                } else {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG1;
                    mdcTagTmp.viewTag = "BFG1";
                    return mdcTagTmp;
                }
            }

            if ("BFG2".equals(area)) {
                if (plcAddress.equals("R_BS[5]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG2_EXIT;
                    mdcTagTmp.viewTag = "BFG2_EXIT";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_BS[158]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG2_FEED;
                    mdcTagTmp.viewTag = "BFG2_FEED";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_B[1]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG2_MANUAL;
                    mdcTagTmp.viewTag = "BFG2_MANUAL";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_HMI_IS[12].0")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG2_GREEN;
                    mdcTagTmp.viewTag = "BFG2_GREEN";
                    return mdcTagTmp;
                } else {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG2;
                    mdcTagTmp.viewTag = "BFG2";
                    return mdcTagTmp;
                }
            }

            if ("BFG3".equals(area)) {
                if (plcAddress.equals("R_BS[5]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG3_EXIT;
                    mdcTagTmp.viewTag = "BFG3_EXIT";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_BS[158]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG3_FEED;
                    mdcTagTmp.viewTag = "BFG3_FEED";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_B[1]")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG3_MANUAL;
                    mdcTagTmp.viewTag = "BFG3_MANUAL";
                    return mdcTagTmp;
                } else if (plcAddress.equals("R_HMI_IS[12].0")) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG3_GREEN;
                    mdcTagTmp.viewTag = "BFG3_GREEN";
                    return mdcTagTmp;
                } else {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG3;
                    mdcTagTmp.viewTag = "BFG3";
                    return mdcTagTmp;
                }
            }

            if ("BFG4".equals(area)) {
                if (plcAddress.equals("Program:P01_BFG_Spraypost.R006_BS_Exit_Conveyor_Run_Cmd".toUpperCase())) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG4_EXIT;
                    mdcTagTmp.viewTag = "BFG4_EXIT";
                    return mdcTagTmp;
                } else if (plcAddress.equals("Program:P01_BFG_Spraypost.R006_BS_Feed_Conveyor_Run_Command".toUpperCase())) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG4_FEED;
                    mdcTagTmp.viewTag = "BFG4_FEED";
                    return mdcTagTmp;
                } else if (plcAddress.equals("PROGRAM:P01_BFG_SPRAYPOST.R005_B_Spraypost_in_Manual".toUpperCase())) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG4_MANUAL;
                    mdcTagTmp.viewTag = "BFG4_MANUAL";
                    return mdcTagTmp;
                } else if (plcAddress.equals("Program:P01_BFG_Spraypost.R005_B_Spraypost_in_Auto".toUpperCase())) {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();

                    mdcTagTmp.type = PlcTagType.BFG4_GREEN;
                    mdcTagTmp.viewTag = "BFG4_GREEN";
                    return mdcTagTmp;
                } else {
                    MdcTagTmp mdcTagTmp = new MdcTagTmp();
                    mdcTagTmp.type = PlcTagType.BFG4;
                    mdcTagTmp.viewTag = "BFG4";
                    return mdcTagTmp;
                }
            }
            if ("OVEN".equals(area)) {
                MdcTagTmp mdcTagTmp = new MdcTagTmp();
                mdcTagTmp.type = "OVEN";
                mdcTagTmp.viewTag = "OVEN_DI";
                return mdcTagTmp;
            }
        }

        if (plcAddress.endsWith("]")) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            if (plcAddress.contains("SYSTEM")) {
                mdcTagTmp.type = PlcTagType.SYSTEM;
                mdcTagTmp.viewTag = "SYSTEM";
            } else {
                mdcTagTmp.type = PlcTagType.DEF;
                mdcTagTmp.viewTag = "DEF";
            }
            return mdcTagTmp;
        }

        if (plcAddress.endsWith(PlcTagType._PROD) ||
                plcAddress.endsWith(PlcTagType._PRODUCTION) ||
                plcAddress.endsWith(PlcTagType._MAINTENANCE)) {

            String tmp = plcAddress;
            if (tmp.startsWith("PROGRAM:")) {
                tmp = tmp.substring("PROGRAM:".length());

                int index = tmp.indexOf('.');

                tmp = tmp.substring(0, index);

                MdcTagTmp mdcTagTmp = new MdcTagTmp();


                mdcTagTmp.section = tmp;
                mdcTagTmp.viewTag = tmp;


                if (plcAddress.endsWith(PlcTagType._PROD)) {
                    mdcTagTmp.type = PlcTagType._PRODUCTION;
                } else if (plcAddress.endsWith(PlcTagType._PRODUCTION)) {
                    mdcTagTmp.type = PlcTagType._PROD;
                } else {
                    mdcTagTmp.type = PlcTagType._MAINTENANCE;
                }

                return mdcTagTmp;

            } else {
                throw new RuntimeException("不识别的plcAddress:" + plcAddress);
            }
        }
        for (String postfix : postfixArray) {
            if (plcAddress.endsWith(postfix)) {
                return getViewTag(shop, area, plcAddress, postfix);
            }
        }


        if (plcAddress.contains("DIGITS")) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();

            mdcTagTmp.viewTag = getViewTag(plcAddress);

            for (String postfix : digitsPostfixArray) {
                if (plcAddress.endsWith(postfix)) {
                    mdcTagTmp.type = postfix;
                    break;
                }
            }

            return mdcTagTmp;
        }

        if (plcAddress.contains("POSTE")) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            mdcTagTmp.viewTag = getViewTag(plcAddress);


            for (String postfix : postePostfixArray) {
                if (plcAddress.endsWith(postfix)) {
                    mdcTagTmp.type = postfix;
                    break;
                }
            }

            return mdcTagTmp;
        }

        if ("FAULT".equals(plcAddress)) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            mdcTagTmp.type = PlcTagType.FAULT;
            mdcTagTmp.viewTag = "FAULT";
            return mdcTagTmp;
        }
        if ("PROGRAM:SECTION1.V_FAULT".equals(plcAddress)) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            mdcTagTmp.type = PlcTagType.V_FAULT;
            mdcTagTmp.viewTag = "V_FAULT";
            return mdcTagTmp;
        }
        if ("PROGRAM:SECTION1.R208_ACTIONS_V_RUN_SYMBOL".equals(plcAddress)) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            mdcTagTmp.type = PlcTagType.V_RUN;
            mdcTagTmp.viewTag = "V_RUN";
            return mdcTagTmp;
        }

        if ("BSW_RUN".equals(plcAddress)) {
            MdcTagTmp mdcTagTmp = new MdcTagTmp();
            mdcTagTmp.type = PlcTagType.BSW_RUN;
            mdcTagTmp.viewTag = "BSW_RUN";
            return mdcTagTmp;
        }

        return unRead();
    }


    private MdcTagTmp getViewTag(String shop, String area, String plcAddress, String postfix) {
        String tmp = plcAddress.substring(0, plcAddress.length() - postfix.length());

        if (tmp.startsWith("PROGRAM:")) {
            tmp = tmp.substring("PROGRAM:".length());

            String[] tmpArray = tmp.split("\\.");

            if (tmpArray[1].startsWith("I_") || tmpArray[1].startsWith("O_")) {
                tmpArray[1] = tmpArray[1].substring(2);
            }

            if (tmpArray[1].startsWith("V_")) {
                tmpArray[1] = tmpArray[1].substring(2);
            }

            if (tmpArray[1].startsWith("R0") || tmpArray[1].startsWith("R1")) {
                int t1 = tmpArray[1].indexOf('_');

                if (t1 > 0) {
                    tmpArray[1] = tmpArray[1].substring(t1 + 1);
                }
            }

            int tmpIndex = tmpArray[1].indexOf('_');

            if (tmpIndex > 0) {
                //像这样的：TC101_EN4 处理为TC101
                tmpArray[1] = tmpArray[1].substring(0, tmpIndex);
            }

            if (tmpArray[1].charAt(tmpArray[1].length() - 1) == '.') {
                tmpArray[1] = tmpArray[1].substring(0, tmpArray[1].length() - 1);
            }

            MdcTagTmp mdcTagTmp = new MdcTagTmp();


            mdcTagTmp.section = tmpArray[0];
            mdcTagTmp.viewTag = tmpArray[1];
            mdcTagTmp.type = postfix;


            return mdcTagTmp;
        }

        int lastIndex = tmp.lastIndexOf('_');


        String viewTag = tmp.substring(lastIndex + 1);

        MdcTagTmp mdcTagTmp = new MdcTagTmp();

        mdcTagTmp.viewTag = viewTag;
        mdcTagTmp.type = postfix;

        return mdcTagTmp;
    }

    private String getViewTag(String plcAddress) {

        int startIndex = plcAddress.indexOf('_');

        if (startIndex < 0) {
            return null;
        }

        int endIndex = plcAddress.indexOf('_', startIndex + 1);

        if (endIndex < 0) {
            return null;
        }

        //System.out.println(plcAddress + ":" + startIndex + "," + endIndex);

        return plcAddress.substring(startIndex + 1, endIndex);

    }


    private MdcTagTmp unRead() {
        return null;
    }


}
